package restaurant.agents;

import jade.core.Agent;
import jade.domain.DFService;
import jade.domain.FIPAException;

import java.util.ArrayList;
import java.util.Random;

import restaurant.behaviours.customer.CustomerBehaviour;
import restaurant.core.CustomerType;
import restaurant.core.Restaurant;
import restaurant.gui.MainJFrame;
import restaurant.gui.Square;

public class CustomerAgent extends Agent {
	
	private static final long serialVersionUID = -807881523005299408L;
	public boolean goalReached = false;
	
	public restaurant.gui.Agent me;
	public Restaurant restaurant;
	
	/**
	 * Indica la performance measure
	 * */
	private int score = 0;
	private int table = -1;
	private CustomerType type;
	private ArrayList<String> orders;
	private ArrayList<String> requests;
	
	Random randomGen = new Random();
	
	public boolean goalReached() {
		return this.goalReached;
	}
	
	@Override
	protected void setup() {
		this.restaurant = Restaurant.getInstance();
		
		MainJFrame m = ((MainJFrame)getArguments()[0]);
		this.me = new restaurant.gui.Agent(0, 0, getAID(), Square.Type.CUSTOMER);
		m.env.agents.add(me);
		
		if(this.type == null) {
			this.type = CustomerType.DIET;
			
			this.requests = new ArrayList<String>();
			int random = Math.abs(randomGen.nextInt()) % 2;
			if (random == 0)
				this.requests.add("secondo di carne");
			else
				this.requests.add("secondo di pesce");
			random = Math.abs(randomGen.nextInt()) % 2;
			if (random == 0)
				this.requests.add("mela");
			else
				this.requests.add("ananas");
		}
		switch (this.type) {
		case GREEDY:
			this.requests = new ArrayList<String>();
			this.requests.add("ananas");
			this.requests.add("mela");
			int random = Math.abs(randomGen.nextInt()) % 2;
			if (random == 0)
				this.requests.add("torta");
			else
				this.requests.add("muffin");
			
			this.requests.add("pesce");
			this.requests.add("carne");
			
			random = Math.abs(randomGen.nextInt()) % 2;
			if (random == 0)
				this.requests.add("pasta");
			else
				this.requests.add("riso");
			break;
		case SPENDTHRIFT:
			this.requests = new ArrayList<String>();
			
			random = Math.abs(randomGen.nextInt()) % 2;
			if (random == 0)
				this.requests.add("mare");
			else
				this.requests.add("monti");
			this.requests.add("pasta");
			this.requests.add("riso");
			this.requests.add("pesce");
			this.requests.add("carne");
			
			random = Math.abs(randomGen.nextInt()) % 2;
			if (random == 0)
				this.requests.add("ananas");
			else
				this.requests.add("mela");
			
			random = Math.abs(randomGen.nextInt()) % 2;
			if (random == 0)
				this.requests.add("torta");
			else
				this.requests.add("muffin");
			
			break;
		case STINGY:
			this.requests = new ArrayList<String>();
			
			random = Math.abs(randomGen.nextInt()) % 2;
			if (random == 0)
				this.requests.add("pesce");
			else
				this.requests.add("carne");
		default:
			break;
		}
		if (this.orders == null)
			this.orders = new ArrayList<String>();
		
		addBehaviour(new CustomerBehaviour(this));
	}

	@Override
	protected void takeDown() {
		try {
			DFService.deregister(this);
		} catch (FIPAException e) {
		}
	}

	public void setTable(int table) {
		this.table = table;
	}

	public Integer getTable() {
		return table;
	}

	public CustomerType getType() {
		return type;
	}
	
	public void setType(CustomerType type) {
		this.type = type;
	}
	
	public ArrayList<String> getOrders() {
		return orders;
	}

	public void setOrders(ArrayList<String> orders) {
		this.orders = orders;
	}
	
	public void setOrder(String order) {
		this.orders.add(order);
	}

	public void setRequests(ArrayList<String> requests) {
		this.requests = requests;
	}

	public ArrayList<String> getRequests() {
		return requests;
	}

	public void setScore(int i) {
		this.score = this.score + i;
		
		this.restaurant.agentsScore.put(getAID(), (double) this.score);
	}
	
	public void setScoreTot(int score) {
		this.score = this.score - score;
	}

	public int getScore() {
		return score;
	}
}